/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.service.payment.provider.cybersource.context;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.martinimix.domain.payment.BillMeLaterAccount;

/**
 * Provides a simple CyberSource request wrapper for Bill Me Later
 * authorization requests.
 * 
 * @author Scott Rossillo
 *
 */
public class BillMeLaterAuthorizationRequest extends OrderPaymentAuthorizationRequest {

	private final static String BML_CARD_TYPE = "028";
	private final static String BML_CARD_EXP_MONTH = "12";
	private final static String BML_CARD_EXP_YEAR = "2021";
	
	public final static String DELIVERY_TYPE_DEFAULT = "default";
	public final static String DELIVERY_TYPE_ELECTRONIC_SOFTWARE= "electronic_software";
	public final static String DELIVERY_TYPE_SERVICE = "service";
	public final static String DELIVERY_TYPE_SHIPPING_AND_HANDLING = "shipping_and_handling";
	
	/**
	 * Creates a new Bill Me Later authorization request.
	 */
	public BillMeLaterAuthorizationRequest() { 
		super();
		
		super.put("card_cardType", BML_CARD_TYPE);
		super.put("card_expirationMonth", BML_CARD_EXP_MONTH);
		super.put("card_expirationYear", BML_CARD_EXP_YEAR);
		
		// only supporting new customers
		super.put("bml_customerTypeFlag", "N");
		
		super.put("bml_productDeliveryTypeIndicator", DELIVERY_TYPE_DEFAULT);
		super.put("bml_itemCategory", "2300"); // for elec or 3000 otherwise 
	}
	
	private String formatDate(Calendar dateOfBirth) {
		
		return (new SimpleDateFormat("yyyy-MM-dd")).format(dateOfBirth.getTime());
	}

	public void setBillMeLaterAccount(BillMeLaterAccount billMeLaterAccount) {
		
		super.put("billTo_dateOfBirth", formatDate(billMeLaterAccount.getDateOfBirth()));
		super.put("billTo_ssn", billMeLaterAccount.getSocialSecurityNumber());
		super.put("card_accountNumber", billMeLaterAccount.getCardNumber());
		super.put("bml_tcVersion", billMeLaterAccount.getTermsAndConditionsVersion());
	}
	
	public void setCustomerRegistrationDate(Calendar customerRegistrationDate) {
		super.put("bml_customerRegistrationDate", formatDate(customerRegistrationDate));
	}
	
	public void setCustomerType(String customerType) {
		super.put("bml_customerTypeFlag", customerType);
	}

	public void setProductDeliveryType(String productDeliveryType) {
		super.put("bml_productDeliveryTypeIndicator", productDeliveryType);
	}

	public void setItemCategory(String itemCategory) {
		super.put("bml_itemCategory", itemCategory); // for elec or 3000 otherwise 
	}
}
